//==========================================================================
// Copyright (c) 2000-2008,  Elastos, Inc.  All Rights Reserved.
//==========================================================================

#ifndef __ELASTOS_KMALLOC_H__
#define __ELASTOS_KMALLOC_H__

#if defined(KERNEL_MEMORY_DETECT)

EXTERN Void KMemInsertIntoHash(const PVoid pv, const UInt32 uSizeOrOrder,
                Boolean bPhysPage = FALSE);
EXTERN Boolean KMemDetachFromHash(PVoid pv, Boolean bPhysPage = FALSE);

#ifndef KMEM_INSERT_INTO_HASH
#define KMEM_INSERT_INTO_HASH \
    do { \
        KMemInsertIntoHash(pv, (UInt32)size); \
    } while(0);

#endif //KMEM_INSERT_INTO_HASH

#ifndef KMEM_DETACH_FROM_HASH
#define KMEM_DETACH_FROM_HASH \
    do { \
        if (!KMemDetachFromHash(pv)) { \
            kprintf("\nERROR: Try to free an invalid block of kernel memory!\n"); \
            DebugBreak(); \
        } \
    } while(0);
#endif //KMEM_DETACH_FROM_HASH

#else

#define KMEM_INSERT_INTO_HASH
#define KMEM_DETACH_FROM_HASH

#endif //KERNEL_MEMORY_DETECT


EXTERN bool_t LockMemoryAllocatorAndDisablePreemption();
EXTERN void UnlockMemoryAllocatorAndRestorePreemption(bool_t bPreemption);

EXTERN_C void *malloc(size_t size);
EXTERN_C void free(void *pv);

void *operator new(size_t size);

void operator delete(void *pv);

void *operator new [](size_t size);

void operator delete [](void *pv);

#endif // __ELASTOS_KMALLOC_H__
